#include<iostream>
#include<cstdio>
#include <cstring>
#include "search.hpp"
#include "load.h"
using namespace std;

vector<Polygon> layout;
Pattern pattern;

int main(int argc, char** argv) {
    clock_t start,end;
    start = clock();
    //cout << "start " << start << endl;
    //处理参数
    string layout_filename = "../public/Pattern_matching_test_case/layout/small.txt";
    string lib_filename = "../public/Pattern_matching_test_case/lib/lib1.txt";
    string output_filename = "ans.txt";
    bool rotate_flag = false;
    int thread_cnt = 1;

    for(int i=1;i<argc;i++)
    {
        switch (argv[i][1]) {
            case 'l':
                if (argv[i][2] == 'a') {
                    i++;
                    layout_filename = argv[i];
                } else {
                    i++;
                    lib_filename = argv[i];
                }
                break;
            case 'w':
                i++;
                if (argv[i][0] == 'Y')
                    rotate_flag = true;
                break;
            case 't':
                i++;
                thread_cnt = atoi(argv[i]);
                break;
            case 'o':
                i++;
                output_filename = argv[i];
                break;
        }
    }
    //cout << "open file1 " << (double)(clock() - start)/CLOCKS_PER_SEC << endl;
    start = clock();

    //读入
    get_layout(layout_filename.c_str(), layout, thread_cnt);

    //cout << "open file 2 " << (double )(clock() - start)/CLOCKS_PER_SEC << endl;
    start = clock();


    FILE *f2 = fopen(lib_filename.c_str(), "rb");
    pattern = get_pattern(f2);
    fclose(f2);

    //cout << "start search " << (double)(clock() - start)/CLOCKS_PER_SEC << endl;
    start = clock();

    //调用search
    Search search = Search(pattern, &layout, rotate_flag, thread_cnt);
    //cout << "hello world" << endl;

    //cout << "start solve " << (double)(clock() - start)/CLOCKS_PER_SEC << endl;
    start = clock();

    search.solve();

    //cout << "end solve " << (double)(clock() - start)/CLOCKS_PER_SEC << endl;
    start = clock();

    FILE *f3 = fopen(output_filename.c_str(), "w");
    search.print_ans(f3);
    fclose(f3);

    //cout << (double)(clock() - start)/CLOCKS_PER_SEC << endl;
    return 0;
}